define([
    'app',
    'mlab-audio',
    'js/api/OrderAPI',
    'js/api/ProductAPI'
], function (app) {
    'use strict';
    app.controller('meilihuiCtrl', ['$scope','$rootScope', '$http', '$stateParams', '$location', 'OrderAPI', 'ProductAPI', 'audioData',
        function($scope, $rootScope, $http, $stateParams, $location, OrderAPI, ProductAPI, audioData) {
            function getProductdata() {
                audioData.setColor('rgba(255,255,255,1)')
                var name = $stateParams.name;
                console.log(name);  
                ProductAPI.dataUrl(name).then(function(data) {
                    console.log(data);   
                    if(data.model.objData.panels.size) {
                        $scope.needRingSize = true;   
                        $scope.ringSizeData = data.model.objData.panels.size.options.ringSize.data;

                    }  
                    var cdn = 'http://cdn.m-lab.cn/';
                    for (var i in data.images) {
                        if (data.images[i]['main']) {
                            $scope.mainImg = cdn + data.images[i]['main'];
                            break;
                        }
                    }
                    function initOrderObj(options, thirdparty){
                        var getPrice = eval(data.priceCount.js);
                        var price = getPrice(options) * 100;
                        var orderObj = {
                            platform: {
                                name: thirdparty
                            },
                            subject: '马良行-' + thirdparty,
                            body: '陌生来信',
                            allSubTotal: price,
                            allQuantity: 1,
                            allDiscountAmount: 0,
                            allShippingCost: 0,
                            allTotal: price,
                            change: 0,
                            item: [],
                            message: $scope.phone.input,
                            note: thirdparty + '订单',
                            address: {
                                name: '许鹏飞',
                                phone: '15900836787',
                                province: {
                                    objectId: '26',
                                    __type: 'Pointer',
                                    className: 'Address_province'
                                },
                                city: {
                                    objectId: '382',
                                    __type: 'Pointer',
                                    className: 'Address_city'
                                },
                                detail: thirdparty + '订单无地址'
                            }
                        };

                        orderObj.item.push({
                            product: {
                                __type: 'Pointer',
                                className: 'Product',
                                objectId: data.objectId
                            },
                            options: options,
                            price: price,
                            priceDiscount: price,
                            subTotal: price,
                            total: price,
                            quantity: 1,
                            shippingCost: 0
                        });
                        return orderObj;
                    }
                    $scope.confirm = function(){
                        if($scope.submitting || $scope.submitted) return;
                        if(!$scope.wavUrl) {
                            alert('请先录制语音');
                            return;
                        }
                        if($scope.needRingSize && !$scope.ringSize){
                            alert('请选择戒围');
                            return;
                        }
                        $scope.submitting = true;
                        $scope.confirmBtn = '提交中';
                        var orderObj = initOrderObj({
                            className : data.model.objData.object,
                            materialName: 'silver',
                            heights: $scope.heights,
                            wavUrl: $scope.wavUrl,
                            ringSize: $scope.ringSize,
                            type: $scope.type
                        }, thirdparty);


                        OrderAPI.anonymousOrder(orderObj).then(function(data) {
                            
                            $scope.confirmBtn = '录音成功';
                            $scope.submitted = true;
                            alert('魅力告白已生成，请于11月18日留意微信的获奖公布');

                        }, function(error) {
                            console.log('下单失败:', error);
                            alert('录音失败，请重新录制')
                            $scope.submitting = false;
                            $scope.confirmBtn = '确定';
                        });
                    }



                });
            }
            getProductdata();


            var thirdparty = '魅力惠';
            var redirectUri = $location.search().redirect_uri || 'm.malianghang.com';
            $scope.type = $location.search().type;

            function validatemobile(mobile) {
               if(mobile.length==0) 
               { 
                  alert('请输入手机号码！'); 
                  return false; 
               }     
               if(mobile.length!=11) 
               { 
                   alert('请输入有效的手机号码！'); 
                   return false; 
               } 
                
               var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
               if(!myreg.test(mobile)) 
               { 
                   alert('请输入有效的手机号码！'); 
                   return false; 
               } 
               return true;
            } 

            $scope.phoneSubmited = false; 
            $scope.phone = {};
            $scope.phone.input = '';
            $scope.getPhone = function () {
                if(validatemobile($scope.phone.input)) {
                    $scope.phoneSubmited = true
                }
                    //closePhoneDialog();

            }




            


            $scope.submitting = false;
            $scope.submitted = false;
            $scope.confirmBtn = '确定';


            if($rootScope.waveformEvent1) $rootScope.waveformEvent1();
            $rootScope.waveformEvent1 = $rootScope.$on('waveform', function(event, waveHeights, data, wavUrl, localId, warning){
                $scope.heights = data;
                $scope.wavUrl = wavUrl;
            });
        }])

});
